/*
 * TMap Designer - A Thematic Map design software
 * Copyright (C) 2006 World Health Organization
 * http://www.who.int
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */
package who.tmap.ui.util;

import java.awt.Container;
import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import org.apache.log4j.Logger;

import who.tmap.bo.Annotations;
import who.tmap.bo.BizObjects;
import who.tmap.bo.impl.BizObjectFactory;
import who.tmap.common.TMapException;
import who.tmap.dao.AnnotationsDAO;
import who.tmap.dao.impl.DAOFactory;

/**
 * A helper dedicated to annotations display
 * @author stl
 *
 */
public class AnnotationsUIHelper extends BaseUIHelper {

    private static Logger logger = Logger.getLogger(AnnotationsUIHelper.class);
    
    private MiscUIHelper miscUIHelper =new MiscUIHelper();
    
	public void saveAnnotations( Container container, File file, Annotations annotations ) {
        if ( file != null ) {
            save( container, file, annotations );
        } else {
        	saveAnnotationsAs( container, annotations );
        }		

	}    
    
	private File save( Container container, File file, Annotations annotations ) {
		try {
			AnnotationsDAO dao =(AnnotationsDAO)DAOFactory.getInstance().get( BizObjects.ANNOTATIONS );
	        dao.save( file.getAbsolutePath(), annotations );
	        annotations.setFile( file );
	        
	        return file;
        } catch (TMapException e) {
            miscUIHelper.displayErrorMessage( container, e );
        }         
        
        return null;
	}
	
	public void saveAnnotationsAs( Container container, Annotations annotations ) {
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.setSelectedFile( new File( "*" + FileChooserFilter.ANNOTATIONS_EXT ));
		fileChooser.addChoosableFileFilter( new FileChooserFilter( FileChooserFilter.ANNOTATIONS_EXT ) );
		fileChooser.setCurrentDirectory( getTMapDir( container, FileChooserFilter.ANNOTATIONS_DIR ) );
		
		// Save file dialog.
		int retVal =fileChooser.showSaveDialog(container);
		
		if ( retVal == JFileChooser.APPROVE_OPTION ) {
			File file =fileChooser.getSelectedFile();
		    logger.debug( "selectedFile=[" + file + "]" );
		    setAnnDir( file.getParentFile() );

	        if ( file.exists() ) {

	            int n =JOptionPane.showConfirmDialog( container,
	                    "This file already exists. Do you want to erase it?",
	                    "Save Annotations",
	                    JOptionPane.YES_NO_OPTION);
	            if ( n == JOptionPane.NO_OPTION ) {
	                file =null;
	            }
	        }
	        
	        if ( file != null ) {
	            save( container, file, annotations );
	        }

		}
		
	}	    
	
	public Annotations openAnnotations( Container container )  {
		Annotations annotations =null;
		JFileChooser fileChooser = new JFileChooser();
		fileChooser.addChoosableFileFilter( new FileChooserFilter( FileChooserFilter.ANNOTATIONS_EXT ) );
		
		fileChooser.setCurrentDirectory( getTMapDir( container, FileChooserFilter.ANNOTATIONS_DIR ) );
		
		// Open file dialog.
		int retVal =fileChooser.showOpenDialog(	container );
		
		if ( retVal == JFileChooser.APPROVE_OPTION ) {		
		
			File file =fileChooser.getSelectedFile();
			setAnnDir( file.getParentFile() );
		    	    
		    try {
		    	AnnotationsDAO dao =(AnnotationsDAO)DAOFactory.getInstance().get( BizObjects.ANNOTATIONS );		        
		    	annotations =(Annotations)dao.load( file.getAbsolutePath() );
	        } catch (TMapException e1) {
	            miscUIHelper.displayErrorMessage( container, e1 );
	        }
		    
		}
	    
	    return annotations;
	}    
	
	public Annotations newAnnotations( Container container ) {
	    Annotations annotations =null;
        try {
        	annotations = (Annotations)BizObjectFactory.getInstance().get( BizObjects.ANNOTATIONS );
        } catch (TMapException e) {
            miscUIHelper.displayErrorMessage( container, e );
        }
        
        return annotations;
	}

    
}
